clear, close, clc

dt = 0.1;
t = -2:dt:4;
f = sin(t);
plot(t,f)

% exact derivate
dfdt = cos(t);

% plotting
plot(t,f,'k--','LineWidth',1.2)
hold on, grid on
plot(t,dfdt,'k','LineWidth',3)
l1 = legend('Function','Exact Derivate');

%%
% Forward difference scheme
dfdtF = (sin(t+dt)-sin(t))/dt;
% Backwared
dfdtB = (sin(t)-sin(t-dt))/dt;
% Central
dfdtC = (sin(t+dt)-sin(t-dt))/(2*dt);

plot(t,dfdtF,'b','LineWidth',1.2)
plot(t,dfdtB,'g','LineWidth',1.2)
plot(t,dfdtC,'r','LineWidth',1.2)
l2 = legend('Function','Exact Derivate','Forward','Backward','Central');
